package com.zyg.apart.controller;

import com.github.pagehelper.PageInfo;
import com.zyg.apart.model.Flatrepair;
import com.zyg.apart.model.Flats;
import com.zyg.apart.model.Lessee;
import com.zyg.apart.model.Logistics;
import com.zyg.apart.service.FlatrepairService;
import com.zyg.apart.service.FlatsService;
import com.zyg.apart.service.LogisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

/**
 * 修缮控制层
 */

@Controller
@RequestMapping("/repair")
public class FlatrepairController {
	@Autowired
	private FlatrepairService flatrepairService;
	@Autowired
	private LogisticsService logisticsService;
	@Autowired
	private FlatsService flatsService;
	@RequestMapping("/list")
	public String list(Integer currentPage, Model model){
		if (currentPage == null){
			currentPage = 1;
		}
		PageInfo<Map<String,Object>> repairs = flatrepairService.getAllRepair(currentPage);
		model.addAttribute("repairs",repairs);
		return "repair/list";
	}

	@RequestMapping("/detail")
	public String detail(Model model,Integer id){
		Map<String,Object> flatrepair = flatrepairService.getRepairById(id);
		model.addAttribute("flatrepair",flatrepair);
		return "repair/detail";
	}

	@RequestMapping("/toEdit")
	public String toEdit(Model model, Integer id){
		Map<String,Object> flatrepair = flatrepairService.getRepairById(id);
		List<Logistics> logs = logisticsService.getAllLogName();
		model.addAttribute("flatrepair",flatrepair);
		model.addAttribute("logs",logs);
		return "repair/edit";
	}

	@RequestMapping("/edit")
	public String edit(Flatrepair flatrepair){
		System.out.println(flatrepair);
		int i = flatrepairService.editRepair(flatrepair);
		return "forward:list";
	}

	@RequestMapping("/del")
	public String del(Integer id){
		int i = flatrepairService.delRepairById(id);
		return "forward:list";
	}

	@RequestMapping("/toAdd")
	public String toAdd(Model model){
		List<Flats> flats = flatsService.getAllName();
		List<Logistics> logs = logisticsService.getAllLogName();
		model.addAttribute("flats",flats);
		model.addAttribute("logs",logs);
		return "repair/add";
	}

	@RequestMapping("/add")
	public String add(Flatrepair flatrepair){
		int i = flatrepairService.addRepair(flatrepair);
		return "forward:list";
	}



}
